/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.siagefi.controller;

import br.siagefi.model.Banco;
import br.siagefi.service.BancoService;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;



/**
 *
 * @author Paulo Lavarini
 */
@ManagedBean
@SessionScoped
public class BancoBean implements Serializable{
    
   private Banco banco;
    private final BancoService service;
    private List<Banco> bancos;
    
    private int contRegistro;

    public BancoBean() {
        this.banco = new Banco();
        this.service = new BancoService();
        this.carregarLista();
    }
    
     public final void carregarLista(){
         this.bancos = service.listar();
    }
    
    public String gravar(){
        service.gravar(this.getBanco());
        this.limparFormularioDoJsf();
        this.carregarLista();
        cont();
        return "banco?faces-redirect=true";
    }
    
    public String alterar(Banco banco){
        this.setBanco(banco);
        return "incluirBanco";
    }
    
     public String remover(){
        service.remover(this.getBanco());
        this.limparFormularioDoJsf();
        this.carregarLista();
        cont();
        return "banco";
    }
     
    public String remover(Banco banco){
        service.remover(banco);
        this.limparFormularioDoJsf();
        this.carregarLista();
        cont();
        return "banco";
    }
  
    public void limparFormularioDoJsf(){
        this.banco = new Banco();
    }
    
    private void cont(){
        contRegistro = 0;
        for (Banco b : bancos) {
            contRegistro++;
        }
    }

    public Banco getBanco() {
        return banco;
    }

    public void setBanco(Banco banco) {
        this.banco = banco;
    }

    public List<Banco> getBancos() {
        return bancos;
    }

    public void setBancos(List<Banco> bancos) {
        this.bancos = bancos;
    }

    public int getContRegistro() {
        return contRegistro;
    }

    public void setContRegistro(int contRegistro) {
        this.contRegistro = contRegistro;
    }

}
